package com.example.logprocess.Job;

import com.example.logprocess.model.LogItem;
import com.example.logprocess.processors.LogProcessor;
import com.example.logprocess.repository.entity.LogEntity;
import com.example.logprocess.repository.convert.LogConvert;
import com.example.logprocess.repository.maper.LogMapper;
import com.example.logprocess.writers.LogWriter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.DefaultJobParametersValidator;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

@Slf4j
@Configuration
@EnableBatchProcessing
@RequiredArgsConstructor
public class LogJob{
    public final JobBuilderFactory jobBuilderFactory;
    public final StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job importUserJob(Step toMysqlStep) {
        return jobBuilderFactory.get("log-job")
                .incrementer(new RunIdIncrementer())
//                .validator(defaultJobParametersValidator()) // 参数校验
                .start(toMysqlStep) //执行步骤
                .build();
    }

    @Bean("toMysqlStep")
    public Step toMysql(ItemWriter<LogEntity> writer,ItemProcessor<LogItem,LogEntity> processor) {
        return stepBuilderFactory.get("toMysql")
//                .tasklet(tasklet)
                .<LogItem,LogEntity>chunk(3)
                .reader(reader())
                .processor(processor)
                .writer(writer)
                .build();
    }

    /**
     *  参数校验器
     * @return
     */
    @Bean
    public DefaultJobParametersValidator defaultJobParametersValidator(){
        DefaultJobParametersValidator validator = new DefaultJobParametersValidator();
        validator.setRequiredKeys(new String[]{"day"});
        return validator;
    }

    @Bean
    public FlatFileItemReader<LogItem> reader() {
        return new FlatFileItemReaderBuilder<LogItem>()
                .name("personItemReader")
                .resource(new ClassPathResource("sample-data.csv"))
                .lineMapper((line, lineNumber) -> LogItem.parseLogEntry(line))
                .targetType(LogItem.class)
                .build();
    }

    @Bean
    public ItemProcessor<LogItem,LogEntity> itemProcessor(LogConvert convert){
        return new LogProcessor(convert);
    }

    @Bean
    public ItemWriter<LogEntity> writer(LogMapper mapper){
        return new LogWriter(mapper);
    }
}
